var express = require('express')
var router = express.Router()
var models = require('../models')
var Op = models.Sequelize.Op
var db = require('../models')
/* GET users listing. */
// 查找所有权限对应的角色的信息 或者 查找被选择的角色的权限信息
router.get('/', async function (req, res, next) {
    try {
        if (req.query.roleId) {
            var roleId = req.query.roleId
            var rights = await models.Role_Right.findAll({
                where: { [Op.and]: [{ roleId: `${roleId}` }] },
                order: [['id', 'ASC']],
                include: {
                    model: models.Right
                }
            })
            res.json({ rights })
        } else if (req.query.rightId) {
            var rightId = req.query.rightId
            var roles = await models.Role_Right.findAll({
                where: { [Op.and]: [{ rightId: `${rightId}` }] },
                order: [['id', 'ASC']],
                include: {
                    model: models.Role
                }
            })
            res.json({ roles })
        } else {
            var role_rights = await models.Role_Right.findAll({
                order: [['id', 'ASC']]
            })
            res.json({ role_rights })
        }
    } catch (e) {
        console.log(e);
    }
})

// 插入新用户的权限
router.post('/:id', async function (req, res, next) {
    const roleId = req.params.id
    const t = await db.sequelize.transaction();
    try {
        const rights = JSON.parse(req.query.data)
        let arr = []
        rights.forEach(item => {
            // item = JSON.parse(item)
            let obj = {
                roleId,
                rightId: `${item}`
            }
            arr.push(obj)
        })
        var Role_Right = await models.Role_Right.bulkCreate(arr)
        await t.commit();
        res.json({ Role_Right })
    } catch (e) {
        console.log(e)
        await t.rollback();
    }
    // const rightId = JSON.parse(req.query.data)
    // const rightId = req.query.data
    // console.log(roleId, rightId);
    // try {
    //     var Role = await models.Role_Right.create({
    //         roleId, rightId
    //     })
    //     res.json({ Role: Role })
    // } catch (e) {
    //     console.log(e)
    // }
})

// 根据roleId修改right
router.put('/:id', async function (req, res, next) {
    const id = req.params.id
    // console.log(id);
    const rights = JSON.parse(req.query.data)
    const t = await db.sequelize.transaction();
    try {
        var row = await models.Role_Right.destroy({
            where: { roleId: id }
        }, { transaction: t })
        // 开启非托管事务后需要进行提交，否则会发生超时
        await t.commit();

        let arr = []
        rights.forEach(item => {
            // item = JSON.parse(item)
            let obj = {
                roleId: id,
                rightId: `${item.rightId}`
            }
            arr.push(obj)
        })
        var Role = await models.Role_Right.bulkCreate(arr)
        res.json({ Role: Role })

    } catch (e) {
        console.log(e)
        await t.rollback();
    }
})

// 删除role_right信息
router.delete('/:id', async function (req, res, next) {
    const roleId = req.params.id
    console.log(roleId)
    if (roleId) {
        try {
            const t = await db.sequelize.transaction();
            var Role = await models.Role_Right.destroy({
                where: {
                    roleId
                }
            })
            await t.commit()
            if (Role) {
                res.json({ msg: '删除成功' })
            }
        } catch (error) {
            console.log(error);
            await t.rollback()
        }
    } else {
        res.json({ msg: '不存在该数据' })
    }
})
module.exports = router
